package com.mobile.check.activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.droidlover.xdroidmvp.base.BaseListActivity;
import cn.droidlover.xdroidmvp.base.BaseRecyclerAdapter;
import cn.droidlover.xdroidmvp.base.RecyclerViewHolder;
import com.mobile.check.beans.TaskInfo;
import com.mobile.check.app.R;
import com.mobile.check.present.LocalTaskListPresent;
import com.mobile.check.dialogs.SubmitTipDialog;

/**
 * 类名: TaskDetailActivity
 * 作者: 李学伟
 * 日期: 2020年10月09日 13:39
 * 描述:
 */
@Deprecated
public class LocalTaskListActivity extends BaseListActivity<TaskInfo, LocalTaskListPresent> {
    @BindView(R.id.rightIv)
    ImageView rightIv;
    @BindView(R.id.submitTv)
    Button submitTv;
    @BindView(R.id.box_empty_tags)
    LinearLayout boxEmptyTags;
    private int taskType;


    @Override
    public void initView() {
        super.initView();
        nodataView = boxEmptyTags;
        titleTv.setText("本地导入");
        mRefreshLayout.setEnableRefresh(false);
        mRefreshLayout.setEnableLoadmore(false);
        rightIv.setVisibility(View.VISIBLE);
        submitTv.setText("导入");
    }

    @Override
    public int getTitleLayoutResId() {
        return 0;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        selectList = new ArrayList<>();
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_taskserverlist;
    }

    List<TaskInfo> selectList = new ArrayList<>();

    @Override
    public BaseRecyclerAdapter<TaskInfo> getAdapter() {
        return new BaseRecyclerAdapter<TaskInfo>(this) {
            @Override
            public int getItemLayoutId(int viewType) {
                return R.layout.item_taskserver;
            }

            @Override
            public void bindData(RecyclerViewHolder holder, final int position) {
                super.bindData(holder, position);
                final TaskInfo info = getItemByPosition(position);
                holder.setText(R.id.deviceNameTv, info.getTask_name());
                if (selectList.contains(info)) {
                    holder.setImageResDrawable(R.id.selectIv, R.drawable.select1);
                } else {
                    holder.setImageResDrawable(R.id.selectIv, R.drawable.select0);
                }
                if (info.isHadDowned()) {
                    holder.setText(R.id.statusTv, "已导入");
                    holder.setVisibility(R.id.statusTv, View.VISIBLE);
                } else {
                    if(info.isErrorFile()){
                        holder.setVisibility(R.id.statusTv, View.INVISIBLE);
                    }else{
                        holder.setText(R.id.statusTv, "文件错误");
                    }

                }
            }
        };
    }

    @Override
    public void itemClick(int position) {
        final TaskInfo info = mBaseAdapter.getItemByPosition(position);
        if (selectList.contains(info)) {
            selectList.remove(info);
        } else {
            selectList.add(info);
        }
        checkSlelectCount();
    }

    private void checkSlelectCount() {
        if (selectList.size() == mBaseAdapter.getItemCount() && mBaseAdapter.getItemCount() != 0) {
            rightIv.setImageResource(R.drawable.allselect1);
        } else {
            rightIv.setImageResource(R.drawable.allselect0);
        }
        if (selectList.size() != 0) {
            submitTv.setVisibility(View.VISIBLE);
        } else {
            submitTv.setVisibility(View.GONE);
        }
        mBaseAdapter.notifyDataSetChanged();
    }


    @OnClick({R.id.rightIv, R.id.submitTv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rightIv:
                if (selectList.size() == mBaseAdapter.getItemCount() && selectList.size() != 0) {
                    selectList.clear();
                } else {
                    selectList.clear();
                    selectList.addAll(mBaseAdapter.getmList());
                }
                checkSlelectCount();
                break;
            case R.id.submitTv:
                check();
                break;
        }
    }

    private void check() {
        boolean hadDowned = false;
        for (TaskInfo info : selectList) {
            if (info.isHadDowned()) {
                hadDowned = true;
                break;
            }
        }
        if (hadDowned) {
            showTip();
        } else {
            getP().save2(selectList);
        }
    }


    private void showTip() {
        SubmitTipDialog submitTipDialog = new SubmitTipDialog(this, new SubmitTipDialog.SlectClickListener() {
            @Override
            public void submitClick() {
                getP().save2(selectList);
            }

            @Override
            public void cancelClick() {

            }
        });
        submitTipDialog.display("温馨提示", "存在已经导入的任务，导入后将覆盖原有任务内容。是否继续导入?", "取消", "导入");
    }

    @Override
    public void getDataFromServer() {
        getP().loadDatasFromFile(taskType);
    }

    @Override
    public void getIntentData() {
        taskType = getIntent().getIntExtra("taskType", 0);
    }

    @Override
    public LocalTaskListPresent newP() {
        return new LocalTaskListPresent();
    }


    public void importSuccess(){
        disLoadDialog();
        setResult(Activity.RESULT_OK);
        finish();
    }

    public static void lunch(Activity context, int taskType) {
        Intent intent = new Intent(context, LocalTaskListActivity.class);
        intent.putExtra("taskType", taskType);
        context.startActivityForResult(intent, 0x001);
    }

    @Override
    public boolean isNotWorkConnect() {
        return false;
    }

}
